'''
audiosystem
Author: Alex Foran
'''

import pygame

'''
A system for playing audio, with a devoted channel for a single "background" track.
'''
class Audiosystem:
	'''
	Creates a new Audiosystem object.
	'''
	def __init__(self):
		# initialize the underlying pygame mixer
		pygame.mixer.init()
		# reserve the first channel as the background channel; most OS's have 16 channels so it shouldn't affect anything
		pygame.mixer.set_reserved(1)
		# set internal variable to the reserved channel
		self.bgm_channel=pygame.mixer.Channel(0)
	
	'''
	Sets the background music. It will repeat until the program closes, the sound is stopped, or a new background music is set.
	
	Params:
	new_bgm - A pygame.sound object to be played
	'''
	def set_bgm(self,new_bgm):
		# don't play it if the loader had an error
		if new_bgm!=None:
			# play sound on the reserved channel, looping infinitely
			self.bgm_channel.play(new_bgm,-1)
	
	'''
	Plays a sound effect on an automatically determined channel, theoretically the channel that was used the longest ago.
	
	Params:
	sfx - A pygame.sound object to be played
	'''
	def play_sfx(self,sfx):
		# don't play if the loader had an error
		if sfx!=None:
			# let pygame take care of all the specifics
			sfx.play()
	
	'''
	Stops all currently playing sounds (theoretically).
	'''
	def stop_all(self):
		pygame.mixer.stop()